const formatTime = (date , type='long')  => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  
  if(type == 'long'){
    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  }else{
    return [year, month, day].map(formatNumber).join('-');
  }
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const gotoUrlPage = e =>{
  let url = e.currentTarget.dataset.url;
  console.log(url);
  if(url!=='#'){
    wx.navigateTo({url: `/pages/${url}`}).catch(e=>{
      wx.switchTab({url: `/pages/${url}`}).catch(e=>{
        wx.showToast({title: '请求地址不存在..',icon: 'none',duration: 2000})
      });
    })
  }else{
    wx.showToast({ title: '功能完善中，敬请期待！', icon: 'none',duration: 2000})
  }
}

const backPage = ()=>{
  wx.navigateBack()
}

function getCurrentDay(){
  var date = new Date();
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  if (month < 10) {
      month = "0" + month;
  }
  if (day < 10) {
      day = "0" + day;
  }
  var nowDate = year + "-" + month + "-" + day;
  return nowDate;
}


module.exports = {
  formatTime: formatTime,
  gotoUrlPage,
  backPage,
  getCurrentDay
}
